package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper , AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        //参数检查
        if(null == channelDto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        channelDto.checkParam();
        //模糊查询
        Page page = new Page(channelDto.getPage() , channelDto.getSize());
        LambdaUpdateWrapper<AdChannel> wrapper = new LambdaUpdateWrapper<>();
        if(StringUtils.isNoneBlank(channelDto.getName())){
            wrapper.like(AdChannel::getName, channelDto.getName());
        }
        IPage<AdChannel> result = page(page , wrapper);
        ResponseResult responseResult = new PageResponseResult(channelDto.getPage() , channelDto.getSize() , (int)result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insertAdChannel(AdChannel adChannel) {
        if(null == adChannel){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    @Override
    public ResponseResult deleteAdChannel(Integer id) {
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel channel = getById(id);
        if (channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID , "频道有效，不能删除");
        }

        //int i = 1/0;

        removeById(id);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateAdChannel(AdChannel adChannel) {
        if(null == adChannel || null == adChannel.getId()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
